package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import control.GameController;

/**
 * Shows buttons for starting a game or quiting a game.
 * Can be extended by adding more buttons with functionality
 * 
 * @author Sam Rang
 *
 */
@SuppressWarnings("serial")
public class Menu extends ViewPanel {
    private ActionListener myActionListener;
    private Image myBackground;
    private GameController myController;
    /**
     * Sets the look and feel of the Menu to function on different
     * operating systems.
     * 
     * @param backgroundFile String representing background image
     * @param gc Controller parent
     */
    public Menu (String backgroundFile, GameController gc) {
        myController = gc;
        myBackground = getBackground(backgroundFile);
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (InstantiationException e) {
            e.printStackTrace();
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        UIManager.put("Button.background", Color.white);
        UIManager.put("Button.foreground", Color.black);
        Font f = new Font("Serif", Font.ITALIC, 14);
        UIManager.put("Button.font", f);
        createListeners();
        setLayout(new BorderLayout());
        addButtons();
    }

    private void addButtons () {
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.CENTER));
        panel.add(makeButton("New Game"));
        panel.add(Box.createRigidArea(new Dimension(0, 5)));
        panel.add(makeButton("Quit"));
        panel.setComponentOrientation(
                ComponentOrientation.LEFT_TO_RIGHT);
        this.add(panel);
    }

    private void createListeners () {
        myActionListener = new ActionListener() {
            @Override
            public void actionPerformed (ActionEvent e) {
                String ea = e.getActionCommand();
                if ("New Game".equals(ea)) {
                    myController.startGame();
                }
                else {
                    System.out.println(ea);
                }
            }
        };
    }

    @Override
    public void paint(Graphics g) {
        super.paintComponents(g);
        super.paint(g);
        Dimension dim = myController.getWindowSize();
        g.drawImage(myBackground, 0, 0, (int)dim.getWidth(), (int)dim.getHeight(),
                0, 0, myBackground.getWidth(this), myBackground.getHeight(this), this);
    }

    private JComponent makeButton (String buttonName) {
        JButton button = new JButton(buttonName);
        button.addActionListener(myActionListener);
        button.setVisible(true);
        return button;
    }
}
